package com.ly.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ly.service.ShopKeeperService;
import com.ly.service.ShopPostService;
import com.ly.service.ShopService;
import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisShop;
import com.system.common.pojo.BisShopPost;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.SysUser;




@Controller
@RequestMapping("ly")
public class ShopController {
	@Autowired
	private ShopService shopService;
	
	@Autowired
	private ShopKeeperService userService;
	@Autowired

	private ShopPostService postService;

	
	@RequestMapping("list")
	public String shoplist(Model model,HttpServletRequest request,@RequestParam(defaultValue="1",value="page")int page){
		//System.out.println(map);//判断map中是否有值
		Map<String,Object> map=initMap(request);
		//System.out.println(map);
		List<BisShop> list=shopService.listAll(map);		
		model.addAttribute("list", list);		
		return "ly/shopList";
	}
	//获取前台的值
	private Map<String,Object> initMap(HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		String sid=request.getParameter("sid");//店铺编号
		String sname=request.getParameter("sname");//店铺名称
		String showStatus=request.getParameter("status");//状态
		String account=request.getParameter("shopUser.account");//店主账号
		String sjname=request.getParameter("sjname");//店铺简称
		String lid=request.getParameter("lid");//营业执照号
		if (sid!=null) {
			map.put("sid", sid);
		}		
		
		if (sname!=null) {
			map.put("sname", sname);
		}		
		
		if (showStatus!=null) {
			map.put("status", showStatus);
		}		
		
		if (account!=null) {
			map.put("account", account);
		}		
		
		if (sjname!=null) {
			map.put("sjname", sjname);
		}		
		
		if (lid!=null) {
			map.put("lid", lid);
		}
		return map;
	}
	/**
	 * 查询所有的店长信息，用于添加店铺信息
	 */
	@RequestMapping(value="queryShopUserAccount",produces="application/json;charset=utf-8")
	@ResponseBody
	public List<BisShopUser> queryShopUserAccount(){
		List<BisShopUser> ShopUserAccountlist=shopService.queryShopUserAccount();
		JsonUtils.toJson(ShopUserAccountlist);
		return ShopUserAccountlist;
	
	}
	/**
	 * 添加店铺信息
	 * @param bisShop
	 * @param files
	 * @param request
	 * @param session
	 * @return
	 */
	
	@RequestMapping("save")
	public String save(BisShop bisShop,@RequestParam("files")MultipartFile[] files,HttpServletRequest request,HttpSession session){
		SysUser user=(SysUser) session.getAttribute("user");
		bisShop.setCreateUserId(user.getUserId());
		bisShop.setModifyUserId(user.getUserId());
		file(files,request,bisShop);
		shopService.insert(bisShop);
		return "redirect:/ly/list";	
		
	}
	/**
	 * 根据主键查询
	 * @param model
	 * @param shopId
	 * @return
	 */
	
	@RequestMapping("queryShopByShopId")
	public String queryShopByShopId(Model model,String shopId){
		BisShop shop=shopService.queryShopByShopId(shopId);
		model.addAttribute("shop", shop);
		return "ly/shopBasicInfo";
	}
	
	
	/**
	 * 修改前给模态框赋值
	 * @param shopId
	 * @return
	 */
	@RequestMapping(value="toupdate",produces="application/json;charset=utf-8")
	@ResponseBody
	public String toupdate(String shopId){
		BisShop bisShop=shopService.queryShopByShopId(shopId);//调用根据id查询的店铺id方法
		String bisshop=JsonUtils.toJsonString(bisShop);//将对象转换成字符串
		return bisshop;//返回json数据
	
	}
	/**
	 * 修改店鋪信息
	 * @param bisShop
	 * @param request
	 * @param file
	 * @return
	 */
	
	@RequestMapping("update")
	public String update(BisShop bisShop,HttpServletRequest request,@RequestParam("files")MultipartFile[] files,HttpSession session){
	
		if (files!=null) {
			file(files, request, bisShop);
		}
		shopService.update(bisShop);
		return "redirect:/ly/list";
	}
	/**
	 * 删除店铺信息
	 * @PathVariable是获取url上数据的。
		@RequestParam获取请求参数的（包括post表单提交）
	 * @param shopkId//店铺id
	 * @return
	 */
	@RequestMapping("delete")
	public String delete(String shopId,HttpServletRequest request){	
		shopService.delete(shopId);
		return "redirect:/ly/list";
	
	}
	
	/**
	 * 文件上传
	 * 
	 * @param files
	 * @param request
	 * @param bisNurseProject
	 */
	public void file(MultipartFile[] files, HttpServletRequest request, BisShop bisShop) {
		if (files != null && files.length > 0) {// 判断file数组不能为空并且长度大于0
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");//设置日期格式
			//判断文件的得到的原始的文件名是否为空，或是否是空串
			if (files[0].getOriginalFilename() != null && !files[0].getOriginalFilename().equals("")) {
				@SuppressWarnings("deprecation")
				String path = request.getRealPath("/upload/shopImage");// 获取项目的路径
				path = path + "/" + sdf.format(new Date());// 存放图片的路径
				File file2 = new File(path);
				if (!file2.exists()) {// 如果当前项目不存在
					file2.mkdirs();// 创建一个新的空目录
				}
				String fileName1 = files[0].getOriginalFilename();// 获取文件名
				File file3 = new File(path, fileName1);// 第一个参数表示第二个参数所在的路径
				if (!file3.exists()) {// 如果当前项目不存在
					try {
						file3.createNewFile();// 创 建一个空的目录
						files[0].transferTo(file3);// 将上传的文件内容拷贝到新文件中
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				bisShop.setShopImage("/upload/shopImage/" + sdf.format(new Date()) + "/" + fileName1);
			}

			if (files[1].getOriginalFilename() != null && !files[1].getOriginalFilename().equals("")) {
				@SuppressWarnings("deprecation")
				String path1 = request.getRealPath("/upload/shopDetailImage");// 获取项目的路径
				path1 = path1 + "/" + sdf.format(new Date());// 存放图片的路径
				File file5 = new File(path1);
				if (!file5.exists()) {// 如果当前项目不存在
					file5.mkdirs();// 创建一个新的空目录
				}
				String fileName2 = files[1].getOriginalFilename();
				File file4 = new File(path1, fileName2);// 第一个参数表示第二个参数所在的路径
				if (!file4.exists()) {// 如果当前项目不存在
					try {
						file4.createNewFile();// 创 建一个空的目录
						files[1].transferTo(file4);// 将上传的文件内容拷贝到新文件中
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				bisShop.setShopDetailImage("/upload/shopDetailImage/" + sdf.format(new Date()) + "/" + fileName2);
			}
		}
	}
	
	/*
	 * 根据店铺id查询该店铺所有员工
	 */	
	@RequestMapping("queryShopUserByShopId")
	public String queryShopUserByShopId(Model model,HttpServletRequest request){
		String shopId=request.getParameter("shopId");
		//System.out.println(shopId);
		Map<String,Object> map1=new HashMap<>();
		Map<String, Object> postmap=this.getParameter(request);
		List<BisShopUser>  shopuserlist=userService.queryShopUserByShopId(shopId, postmap);//根据店铺id查询该店铺信息
		BisShop shop=shopService.queryShopByShopId(shopId);//根据店铺id查询该店铺信息
		List<BisShopPost> shoppost=postService.selectByExample(map1);//查询所有职位信息
		model.addAttribute("shopuserlist", shopuserlist);
		model.addAttribute("shop", shop);
		model.addAttribute("shoppost", shoppost);
		model.addAttribute("shopId", shopId);
		return "ly/showShopUser";
	}
	public Map<String, Object> getParameter(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String baccount = request.getParameter("baccount");
		String bshopName = request.getParameter("bshopName");
		String bpostName = request.getParameter("bpostName");
		String bstatus = request.getParameter("bstatus");
		String busername = request.getParameter("busername");
		String bworkNum = request.getParameter("bworkNum");
		String bmobile = request.getParameter("bmobile");
		String bidno = request.getParameter("bidno");
		if (baccount != null && !baccount.equals("")) {
			map.put("baccount", baccount);
			request.setAttribute("baccount", baccount);
		}
		if (bshopName != null && !bshopName.equals("")) {
			map.put("bshopName", bshopName);
			request.setAttribute("bshopName", bshopName);
		}
		if (bpostName != null && !bpostName.equals("")) {
			map.put("bpostName", bpostName);
			request.setAttribute("bpostName", bpostName);
		}
		if (bstatus != null && !bstatus.equals("")) {
			map.put("bstatus", bstatus);
			request.setAttribute("bstatus", bstatus);
		}
		if (busername != null && !busername.equals("")) {
			map.put("busername", busername);
			request.setAttribute("busername", busername);
		}
		if (bworkNum != null && !bworkNum.equals("")) {
			map.put("bworkNum", bworkNum);
			request.setAttribute("bworkNum", bworkNum);
		}
		if (bmobile != null && !bmobile.equals("")) {
			map.put("bmobile", bmobile);
			request.setAttribute("bmobile", bmobile);
		}
		if (bidno != null && !bidno.equals("")) {
			map.put("bidno", bidno);
			request.setAttribute("bidno", bidno);
		}
		return map;
	}
	

	
		
}
